package wypozyczalnia.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.JoinColumn;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.Type;

@Entity
@Table(name = "movie")
public class Movie extends BaseEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int NORMAL_LENGTH = 255;
	private static final int DESCRIPTION_LENGTH = 4000;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(updatable = false, name = "MOVIE_ID")
	private Integer idMovie;

	@Column(length = 255, nullable = false)
	private String title;

	@Column(length = 255, nullable = false)
	private String production;

	@Column(nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date premiere;

	@Column(length = 4000, nullable = false)
	private String description;

	@ManyToMany(mappedBy = "movie")
	private List<Person> persons;

	@Type(type = "org.hibernate.type.NumericBooleanType")
	@Column(nullable = false)
	private Boolean available;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "movie")
	private List<Hire> hires;

	@OneToMany(cascade = { javax.persistence.CascadeType.ALL }, mappedBy = "movie")
	private List<Photo> photos;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "movie_genre", joinColumns = { @javax.persistence.JoinColumn(name = "MOVIE_ID") }, inverseJoinColumns = { @javax.persistence.JoinColumn(name = "GENRE_ID") })
	private List<Genre> genre;

	public Movie() {
	}

	public Movie(String title, String production, Date premiere,
			String description, List<Person> persons, Boolean available,
			List<Hire> hires, List<Photo> photos) {
		this.title = title;
		this.production = production;
		this.premiere = premiere;
		this.description = description;
		this.persons = persons;
		this.available = available;
		this.hires = hires;
		this.photos = photos;
	}

	public Movie(String title, String production, Date premiere,
			String description, Boolean available) {
		this.title = title;
		this.production = production;
		this.premiere = premiere;
		this.description = description;
		this.persons = new ArrayList();
		this.available = available;
		this.hires = new ArrayList();
		this.photos = new ArrayList();
	}

	public Integer getIdMovie() {
		return this.idMovie;
	}

	public void setIdMovie(Integer idMovie) {
		this.idMovie = idMovie;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<Genre> getGenre() {
		return this.genre;
	}

	public void setGenre(List<Genre> genre) {
		this.genre = genre;
	}

	public String getProduction() {
		return this.production;
	}

	public void setProduction(String production) {
		this.production = production;
	}

	public Date getPremiere() {
		return this.premiere;
	}

	public void setPremiere(Date premiere) {
		this.premiere = premiere;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<Person> getPersons() {
		return this.persons;
	}

	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}

	public Boolean getAvailable() {
		return this.available;
	}

	public void setAvailable(Boolean avalible) {
		this.available = avalible;
	}

	public List<Hire> getHires() {
		return this.hires;
	}

	public void setHires(List<Hire> hires) {
		this.hires = hires;
	}

	public List<Photo> getPhotos() {
		return this.photos;
	}

	public void setPhotos(List<Photo> photos) {
		this.photos = photos;
	}

	public boolean isMovieHired() {
		if (hires != null) {
			for (Hire hire : hires) {
				if (hire.getCancel() == false) {
					return true;
				}
			}
		}

		return false;
	}
	
	public Hire getActiveHire(){
		if (hires != null) {
			for (Hire hire : hires) {
				if (hire.getCancel() == false) {
					return hire;
				}
			}
		}
		return null;
	}
}
